/**
 * Notes: 员工业务逻辑 - 宠物门店预约小程序
 * Date: 2025-01-27
 */

const BaseService = require('./base_service.js');
const EmployeeModel = require('../model/employee_model.js');
const timeUtil = require('../../framework/utils/time_util.js');
const util = require('../../framework/utils/util.js');

class EmployeeService extends BaseService {

	/**
	 * 获取员工列表
	 * @param {string} storeId 门店ID
	 * @param {string} grade 员工等级
	 * @returns {Array} 员工列表
	 */
	async getEmployeeList(storeId = null, grade = null) {
		let where = {
			status: EmployeeModel.STATUS.ONLINE
		};

		// 按门店筛选
		if (storeId) {
			where.storeId = storeId;
		}

		// 按等级筛选
		if (grade) {
			where.title = grade;
		}

		let fields = '_id,storeId,name,title,tags,avatar,status';
		let orderBy = { createTime: -1 };

		return await EmployeeModel.getAll(where, fields, orderBy, 100);
	}

	/**
	 * 获取员工详情
	 * @param {string} employeeId 员工ID
	 * @returns {Object} 员工详情
	 */
	async getEmployeeDetail(employeeId) {
		if (!employeeId) {
			this.AppError('员工ID不能为空');
		}

		let where = {
			_id: employeeId,
			status: EmployeeModel.STATUS.ONLINE
		};

		let employee = await EmployeeModel.getOne(where);
		if (!employee) {
			this.AppError('员工不存在或已离职');
		}

		return employee;
	}

	/**
	 * 添加员工
	 * @param {Object} data 员工数据
	 * @returns {string} 员工ID
	 */
	async addEmployee(data) {
		// 数据验证
		if (!data.storeId || !data.name) {
			this.AppError('门店ID和员工姓名不能为空');
		}

		// 生成员工ID
		let employeeId = util.genRandomString(16);

		let insertData = {
			_pid: 'A00',
			_id: employeeId,
			storeId: String(data.storeId),
			name: data.name,
			title: data.title || '美容师',
			tags: data.tags || [],
			avatar: data.avatar || '',
			status: EmployeeModel.STATUS.ONLINE,
			createTime: timeUtil.time(),
			updateTime: timeUtil.time()
		};

		// 使用模型的insert方法，会自动进行数据清洗
		insertData = EmployeeModel.clearCreateData(insertData);

		await EmployeeModel.insert(insertData);
		return employeeId;
	}

	/**
	 * 更新员工信息
	 * @param {string} employeeId 员工ID
	 * @param {Object} data 更新数据
	 */
	async updateEmployee(employeeId, data) {
		if (!employeeId) {
			this.AppError('员工ID不能为空');
		}

		// 检查员工是否存在
		let employee = await EmployeeModel.getOne({ _id: employeeId });
		if (!employee) {
			this.AppError('员工不存在');
		}

		// 构建更新数据
		let updateData = {
			updateTime: timeUtil.time()
		};

		if (data.name) updateData.name = data.name;
		if (data.title) updateData.title = data.title;
		if (data.tags) updateData.tags = data.tags;
		if (data.avatar) updateData.avatar = data.avatar;
		if (util.isDefined(data.status)) updateData.status = data.status;

		await EmployeeModel.edit({ _id: employeeId }, updateData);
	}

	/**
	 * 删除员工（软删除，设置状态为离职）
	 * @param {string} employeeId 员工ID
	 */
	async deleteEmployee(employeeId) {
		if (!employeeId) {
			this.AppError('员工ID不能为空');
		}

		// 检查员工是否存在
		let employee = await EmployeeModel.getOne({ _id: employeeId });
		if (!employee) {
			this.AppError('员工不存在');
		}

		// 软删除：设置状态为离职
		await EmployeeModel.edit(
			{ _id: employeeId },
			{
				status: EmployeeModel.STATUS.OFFLINE,
				updateTime: timeUtil.time()
			}
		);
	}

	/**
	 * 根据门店获取员工列表
	 * @param {string} storeId 门店ID
	 * @returns {Array} 员工列表
	 */
	async getStoreEmployees(storeId) {
		if (!storeId) {
			this.AppError('门店ID不能为空');
		}

		let where = {
			storeId: storeId,
			status: EmployeeModel.STATUS.ONLINE
		};
		let fields = '_id,name,title,tags,avatar,status';
		let orderBy = { createTime: -1 };

		return await EmployeeModel.getAll(where, fields, orderBy, 100);
	}

}

module.exports = EmployeeService;